#!/bin/sh
_JDK_VERSION=11
_SERVER_NAME="{{server_name}}"
WORK_PATH="{{work_path}}"
_JAR_NAME="{{jar_path}}"

_RUNPID="$WORK_PATH/$_SERVER_NAME.pid"

_RUNJAVA="{{java_home}}/bin/java"

_PACKAGE_URL="{{packageUrl}}"

daemon_quitter_url="{{daemonQuitterUrl}}"
#"http://192.168.0.180:8183/daemon/operatingstatus"
daemon_quitter_appId={{appId}}
daemon_quitter_appKey="{{appKey}}"

_DAEMON_PARAM=""

_DAEMON_PARAM=" -Ddaemon.quitter.url=$daemon_quitter_url "
_DAEMON_PARAM="$_DAEMON_PARAM -Ddaemon.quitter.appId=$daemon_quitter_appId "
_DAEMON_PARAM="$_DAEMON_PARAM -Ddaemon.quitter.appKey=$daemon_quitter_appKey "

cd $WORK_PATH

updatePackage()
{
 echo "成功更新服务包文件"
 wget -O "$_JAR_NAME" "$_PACKAGE_URL"
}
start()
{
  if [ ! -z "$_RUNPID" ]; then
    if [ -f "$_RUNPID" ]; then
      if [ -s "$_RUNPID" ]; then
        echo "启动期间找到的现有PID文件."
        if [ -r "$_RUNPID" ]; then
          PID=`cat "$_RUNPID"`
          ps -p $PID >/dev/null 2>&1
          if [ $? -eq 0 ] ; then
            echo "$_SERVER_NAME 似乎仍在运行,对应的PID为$PID.启动终止."
            echo "如果以下进程不是 $_SERVER_NAME 进程, 请删除PID文件[$_RUNPID]后重试:"
            ps -f -p $PID
            return
          else
            echo "删除/清除陈旧的PID文件."
            rm -f "$_RUNPID" >/dev/null 2>&1
            if [ $? != 0 ]; then
              if [ -w "$_RUNPID" ]; then
                cat /dev/null > "$_RUNPID"
              else
                echo "无法删除或清除过时的PID文件. 启动中止."
                return
              fi
            fi
          fi
        else
          echo "无法读取PID文件. 启动终止."
          return
        fi
      else
        rm -f "$_RUNPID" >/dev/null 2>&1
        if [ $? != 0 ]; then
          if [ ! -w "$_RUNPID" ]; then
            echo "无法删除或写入空PID文件. 启动终止."
              return
          fi
        fi
      fi
    fi
  fi

  _CONSOLE_LOG=$WORK_PATH/logs/console`date +%Y-%m-%d`.log

  nohup $_RUNJAVA  -Dlogging.path=$WORK_PATH/logs $_DAEMON_PARAM  -Duser.home=$WORK_PATH  -jar $_JAR_NAME --spring.config.location=$WORK_PATH/application.yml >> $_CONSOLE_LOG 2>&1 & echo $! > $_RUNPID
  echo $_RUNJAVA  -Dlogging.path=$WORK_PATH/logs $_DAEMON_PARAM -Duser.home=$WORK_PATH  -jar $_JAR_NAME --spring.config.location=$WORK_PATH/application.yml
  echo "启动完成,可以执行以下命令查看日志..."
  echo "    tail -f $_CONSOLE_LOG"
}

stop()
{
  SLEEP=5
  if [ ! -z "$_RUNPID" ]; then
    if [ -f "$_RUNPID" ]; then
      if [ -s "$_RUNPID" ]; then
        kill -0 `cat "$_RUNPID"` >/dev/null 2>&1
        if [ $? -gt 0 ]; then
          echo "找到PID文件，但未找到匹配的进程或当前用户没有权限停止该进程.停止中止."
          return
        fi
      else
        echo "PID文件为空，已被忽略。"
      fi
    else
      echo "已设置 $_RUNPID 但指定的文件不存在. $_SERVER_NAME 正在运行?停止终止."
      return
    fi
  fi


  # stop failed. Shutdown port disabled? Try a normal kill.
  if [ ! -z "$_RUNPID" ]; then
	  echo "停止命令失败.试图通过OS信号发出停止信号的信号."
	  kill -15 `cat "$_RUNPID"` >/dev/null 2>&1
	  rm -f "$_RUNPID" >/dev/null 2>&1
      echo "已使用kill强制杀死进程"
  fi
}




if [ "$1" = "start" ] ; then
	start
elif [ "$1" = "stop" ]; then
	stop
elif [ "$1" = "restart" ]; then
	stop
	start
elif [ "$1" = "update" ]; then
	updatePackage
elif [ "$1" = "-h" ]; then
  # $0 是脚本名称
  echo "Usage: $0 ( commands ... )"
  echo "commands:"
  echo "  start             启动服务"
  echo "  stop              停止服务"
  echo "  update            更新服务包"
  exit 1

else
	start
fi

